//
//  NSObject+DrawLine.m
//  高仿暴走斗图
//
//  Created by key on 16/3/15.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "NSObject+DrawLine.h"
#import "UIColor+Hex.h"

@implementation NSObject (DrawLine)

+ (void)drawLineWithRect:(CGRect)rect lineColor:(UIColor *)lineColor alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth lineType:(kDrawLineType)lineType {
    
    switch (lineType) {
        case kDrawLineTypeAll: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeTop: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeLeft: {
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeRight: {
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeBottom: {
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeNoneTop: {
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeNoneLeft: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeNoneRight: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeNoneBottom: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeTopAndLeft: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeTopAndRight: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeLeftAndRight: {
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeTopAndBottom: {
            [NSObject drawTopLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            
        }
            break;
        case kDrawLineTypeLeftAndBottom: {
            [NSObject drawLeftLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
        case kDrawLineTypeBottomAndRight: {
            [NSObject drawRightLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
            [NSObject drawBottomLineWithFrame:rect color:lineColor alpha:alpha lWidth:lWidth];
        }
            break;
            
        default:
            break;
    }
}

//划线，顶部
+ (void)drawTopLineWithFrame:(CGRect)frame color:(UIColor *)color alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth {
    
    [NSObject drawLineWithStartPoint:CGPointMake(frame.origin.x, frame.origin.y)
                            endPoint:CGPointMake(frame.size.width, frame.origin.y)
                               color:color
                               alpha:alpha
                              lWidth:lWidth];
}

//划线，左边
+ (void)drawLeftLineWithFrame:(CGRect)frame color:(UIColor *)color alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth {
    
    [NSObject drawLineWithStartPoint:CGPointMake(frame.origin.x, frame.origin.y)
                            endPoint:CGPointMake(frame.origin.x, frame.size.height)
                               color:color
                               alpha:alpha
                              lWidth:lWidth];
}

//划线，右边
+ (void)drawRightLineWithFrame:(CGRect)frame color:(UIColor *)color alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth {
    
    [NSObject drawLineWithStartPoint:CGPointMake(frame.size.width, frame.origin.y)
                            endPoint:CGPointMake(frame.size.width, frame.size.height)
                               color:color
                               alpha:alpha
                              lWidth:lWidth];
}

//划线，底部
+ (void)drawBottomLineWithFrame:(CGRect)frame color:(UIColor *)color alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth {
    
    [NSObject drawLineWithStartPoint:CGPointMake(frame.origin.x, frame.size.height)
                            endPoint:CGPointMake(frame.size.width, frame.size.height)
                               color:color
                               alpha:alpha
                              lWidth:lWidth];
}


+ (void)drawLineWithStartPoint:(CGPoint)startpoint endPoint:(CGPoint)endPoint color:(UIColor *)color alpha:(CGFloat)alpha lWidth:(CGFloat)lWidth {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, lWidth);                                                 //线条的粗细
    
    CGFloat red     = [color red];
    CGFloat green   = [color green];
    CGFloat blue    = [color blue];
    CGContextSetRGBStrokeColor(context, red, green, blue, alpha);   //线条颜色
    CGContextMoveToPoint(context,startpoint.x,startpoint.y);                                //起点
    CGContextAddLineToPoint(context,endPoint.x,endPoint.y);                                 //终点
    
    CGContextStrokePath(context);
}


@end
